package com.hsgene.discount.service.impl;

import com.hsgene.common.DeleteFlagType;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.discount.condition.DiscountCondition;
import com.hsgene.discount.constants.DiscountTypeStatus;
import com.hsgene.discount.constants.UseTargetType;
import com.hsgene.discount.domain.GeneticDiscount;
import com.hsgene.discount.dto.GeneticDiscountDto;
import com.hsgene.discount.exception.DiscountErrorCode;
import com.hsgene.discount.persistence.GeneticDiscountMapper;
import com.hsgene.discount.service.DiscountService;
import com.hsgene.discount.service.api.GeneratorService;
import com.hsgene.generator.dto.GeneratorDto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @desc:优惠券
 * @author: fuxiang
 * @date: 2018/10/11 14:55
 */
@Service
public class DiscountServiceImpl implements DiscountService {
    private final static Logger LOGGER = LogManager.getLogger(DiscountServiceImpl.class);
    @Autowired
    GeneticDiscountMapper geneticDiscountMapper;
    @Resource
    GeneratorService generatorService;
    @Override
    public ApiResult<GeneticDiscount> addDiscount(GeneticDiscount geneticDiscount) {
        geneticDiscount.check();
        GeneticDiscount discountByDiscount=geneticDiscountMapper.getByDiscount(geneticDiscount);
        if(discountByDiscount!=null){
            return ApiResult.fail(DiscountErrorCode.COUPON_ALREADY_EXISTS);
        }
        ApiResult<GeneratorDto> generator = generatorService.getGenerator(null, null);
        if(!generator.isSucc()){
            return ApiResult.fail(generator.getError());
        }
        geneticDiscount.setId(generator.getData().getSerialNumber());
        geneticDiscount.setCreateDateTime(new Date());
        geneticDiscount.setUpdateDateTime(geneticDiscount.getCreateDateTime());
        geneticDiscount.setDeleteFlag(DeleteFlagType.NOT_DELETED.getCode());
        geneticDiscountMapper.insertSelective(geneticDiscount);
        return ApiResult.succ(geneticDiscount);
    }

    @Override
    public ApiResult<Pagination<GeneticDiscountDto>> getList(DiscountCondition condition) {
        int totalCount=geneticDiscountMapper.getCount(condition);
        Pagination<GeneticDiscountDto> pagination = Pagination.newInstance(condition.getLimitHelper(), totalCount);
        if (totalCount > 0) {
            List<GeneticDiscount> discountList = geneticDiscountMapper.queryList(condition);
            List<GeneticDiscountDto> discountDtos=new ArrayList<>();
            int rowNumber = (condition.getLimitHelper().getPage() - 1) * condition.getLimitHelper().getSize();
            for (int i = 0, n = discountList.size(); i < n; i++) {
                GeneticDiscount geneticDiscount = discountList.get(i);
                GeneticDiscountDto geneticDiscountDto = new GeneticDiscountDto();
                geneticDiscountDto.setNumber(rowNumber + i + 1);
                geneticDiscountDto.setTypeDesc(DiscountTypeStatus.getEnum(geneticDiscount.getType()).getDesc());
                BeanUtils.copyProperties(geneticDiscount,geneticDiscountDto);
                discountDtos.add(geneticDiscountDto);
            }
            pagination.setResult(discountDtos);
        } else {
            pagination.setResult(new ArrayList<>());
        }
        return ApiResult.succ(pagination);
    }

    @Override
    public ApiResult deleteDiscount(String id) {
        GeneticDiscount geneticDiscount = new GeneticDiscount();
        geneticDiscount.setId(id);
        geneticDiscount.setDeleteFlag(DeleteFlagType.DELETE.getCode());
        int i = geneticDiscountMapper.updateByPrimaryKeySelective(geneticDiscount);
        if(i<1){
            return ApiResult.fail(DiscountErrorCode.OPERATION_FAILED);
        }
        return ApiResult.succ();
    }

    @Override
    public ApiResult deleteDiscountMore(List<GeneticDiscount> discountList) {
        geneticDiscountMapper.deleteDiscountMore(discountList);
        return ApiResult.succ();
    }
}
